// pages/mine/mine.js
const util = require('../utils/util.js')
const db = wx.cloud.database()
const cmd = db.command
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        record: null,
        queryInput: '',
        isScr: false,
        scrHeight: 600,
        touchstart: 0,
        touchend: 0,
        clickNum: 1,
        lastTapTime: 0
    },
    onShareAppMessage: function () {
        return {
            title: '分享六爻策给你的伙伴吧',
            path: '/pages/index/index',
            imageUrl: '/images/6YCSHARE.png'
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

        console.log("是否再次onShow");

        this.setData({
            queryInput: ''
        })

        wx.showLoading({
            title: '加载中',
        })

        var rate = 0
        var rpxH = 0

        wx.getSystemInfo({
            success: function (res) {
                console.log(res);
                rate = 750 / res.screenWidth
                rpxH = rate * res.screenHeight
                console.log(rpxH);
            }
        })
        this.rate = rate
        this.rpxH = rpxH

        // 再通过setData更改Page()里面的data，动态更新页面的数据
        this.setData({
            scrHeight: parseInt(rpxH * 0.6)
        })

        db.collection('LY_RECORD').get().then(res => {
            console.log(res.data);
            var list = res.data
            for (var i = 0; i < list.length; i++) {
                list[i].seq = i + 1
                list[i].time = util.formatTime(list[i].time)
            }
            //是否显示进度条
            this.showScr(list)

            this.setData({
                record: list
            })
        })
        wx.hideLoading();
    },
    showScr: function (list) {
        var size = list.length

        console.log(this.rpxH);

        var oneLine = 120

        console.log((size * oneLine) > (this.rpxH * 0.6));

        if (size > 8 || (size * oneLine) > (this.rpxH * 0.6)) {
            this.setData({
                isScr: true
            })
        } else {
            this.setData({
                isScr: false
            })
        }
    },
    getInputValue: function (e) {
        this.setData({
            queryInput: e.detail.value
        })
    },
    query: function () {
        console.log("有没得值" + this.data.queryInput)

        wx.showLoading({
            title: '加载中',
        })
        db.collection("LY_RECORD").where(cmd.or([{
            name: db.RegExp({
                regexp: '.*' + this.data.queryInput + '.*',
                options: 'i'
            })
        },
        {
            guaname: db.RegExp({
                regexp: '.*' + this.data.queryInput + '.*',
                options: 'i'
            })
        }
        ])).orderBy("time", "desc").get().then(res => {
            console.log(res.data);
            var list = res.data
            for (var i = 0; i < list.length; i++) {
                list[i].seq = i + 1
                list[i].time = util.formatTime(list[i].time)
            }
            //是否显示进度条
            this.showScr(list)
            this.setData({
                record: list
            })
        })
        wx.hideLoading();
    },
    btnclick: function (e) {
        var me = this;
        this.clickTime(e)
        if (me.data.clickNum == 2) {//想定义双击就换成2，三击就换成3...
            console.log("点击" + 2 + "次，进入");//这里写想要执行的代码块
            this.queryOne(e, 2) //响应事件
        }
    },
    clickTime: function (e) {
        var me = this;
        var curTime = e.timeStamp //获取时间戳
        var lastTime = me.data.lastTapTime; //第一次获取为零
        console.log("上一次点击时间：" + lastTime)
        console.log("这一次点击时间：" + curTime)

        if (curTime - lastTime < 500) {
            me.setData({ clickNum: me.data.clickNum + 1 })
        } else {
            me.setData({ clickNum: 1 })
        }
        console.log("me.data.clickNum:", me.data.clickNum);
        me.setData({
            lastTapTime: curTime
        })
    },
    queryOne: function (e, num) {
        console.log(e.currentTarget)
        console.log(e.currentTarget.dataset.id)
        var id = e.currentTarget.dataset.id

        wx.showLoading({
            title: '加载中',
        })
        db.collection('LY_RECORD').doc(id).get().then(res => {
            var obj = res.data
            console.log(obj);
            var yaoList = obj.yaolist
            var nowDate = obj.time
            var longitude = obj.longitude

            //将值放到内存里面
            app.globalData.yaoList = yaoList
            app.globalData.nowDate = nowDate
            app.globalData.longitude = longitude

            if (num == 2) {
                wx.switchTab({
                    url: '../paipan/paipan'
                })
            }
        })
        wx.hideLoading();
    },
    touchstart: function (e) {
        this.setData({
            touchstart: e.timeStamp
        })
    },
    touchend: function (e) {
        this.setData({
            touchend: e.timeStamp
        })
        if ((this.data.touchend - this.data.touchstart) >= 1000) {  /*长按5秒*/
            this.delete(e)
        }
    },
    delete: function (e) {
        var self = this
        console.log(e.currentTarget.dataset.id);
        var id = e.currentTarget.dataset.id
        wx.showModal({
            title: '提示',
            content: '是否删除这条记录？',
            success(res) {
                if (res.confirm) {
                    console.log('用户点击确定')
                    wx.showLoading({
                        title: '加载中',
                    })
                    db.collection('LY_RECORD').doc(id).remove().then(res => {
                        console.log(res)
                        //重新查询所有的
                        self.onShow()
                    })
                    wx.hideLoading();
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
})